/**
 * 
 */
package com.yunyike.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yunyike.mapper.StockMapper;
import com.yunyike.model.Stock;
import com.yunyike.service.IStockService;
import com.yunyike.utils.JsonUtils;

/**
 * @author zhengqiang.shi
 * 2016-4-18 上午9:57:04
 */
@Service("StockServiceImpl")
public class StockServiceImpl implements IStockService{

	private static Logger log = LoggerFactory.getLogger(StockServiceImpl.class);
	
	@Autowired
	private StockMapper stockMapper;
	
	public Map<String, Stock> getStock(List<String> merchandises) {
		
		log.info("getStock request param:{}",JsonUtils.ObjectAsString(merchandises));
		
		if(merchandises == null || merchandises.isEmpty()){
			log.info("request param is null.<getStock>");
			return null;
		}
		
		List<Stock> stocks = stockMapper.selectByMerchandiseId(merchandises);
		if(stocks == null || stocks.isEmpty()){
			log.info("stocks is null.");
			return null;
		}
		
		Map<String, Stock> transfer2Map = transfer2Map(stocks);
		
		return transfer2Map;
	}

	private Map<String, Stock> transfer2Map(List<Stock> stocks){
		
		Map<String, Stock> stockMap = new HashMap<String, Stock>();
		
		for (Stock stock : stocks) {
			
			if(stock == null){
				continue;
			}
			stockMap.put(stock.getStockMerchandiseId(), stock);
		}
		
		return stockMap;
	}

	public int addStock(Stock stock) {
		return stockMapper.insertSelective(stock);
	}

	/**
	 * 更新库存信息
	 */
	public int updateStock(Stock stock) {
		return stockMapper.updateByPrimaryKeySelective(stock);
	}
}
